﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm
{
    class cManagerItem
    {
        public citem[] m_arrayItem;
        int m_iTemp;
        Random m_Rd = new Random();
        const int c_iMaxItem = 10;
        #region CatHinh
        public Rectangle item1 = new Rectangle(0, 0, 45, 60);
        public Rectangle item2 = new Rectangle(45, 0, 45, 60);
        public Rectangle item3 = new Rectangle(90, 0, 45, 60);
        public Rectangle item4 = new Rectangle(135, 0, 45, 60);
        public Rectangle item12 = new Rectangle(0, 0, 45, 40);
        public Rectangle item22 = new Rectangle(45, 0, 45, 40);
        public Rectangle item32 = new Rectangle(90, 0, 45, 40);
        public Rectangle item42 = new Rectangle(135, 0, 45, 40);
        public Rectangle item11 = new Rectangle(0, 40, 45, 40);
        public Rectangle item21 = new Rectangle(45, 40, 45, 40);
        public Rectangle item31 = new Rectangle(90, 40, 45, 40);
        public Rectangle item41 = new Rectangle(135, 40, 45, 40);
        #endregion

        public cManagerItem(ContentManager content)
        {
            m_arrayItem = new citem[c_iMaxItem];
            for (int i = 0; i < c_iMaxItem; i++)
            {
                m_arrayItem[i] = new citem(content);
            }
        }

        public void InitItem(int[, ,] map, int ID, int _row, int _column)
        {
            m_iTemp = 0;
            m_iTemp = m_Rd.Next(0, 30);
            map[ID, _row, _column] = 0;
            foreach (citem Item in m_arrayItem)
            {
                if (map[ID, _row, _column] == 0 && (!Item.m_bAlive))
                {
                    if ((m_iTemp >= 15) && (m_iTemp < 20))
                    {
                        map[ID, _row, _column] = 7;
                    }
                    else
                    {
                        if ((m_iTemp >= 20) && (m_iTemp < 24))
                        {
                            map[ID, _row, _column] = 8;
                        }
                        else
                        {
                            if ((m_iTemp >= 24) && (m_iTemp < 27))
                            {
                                map[ID, _row, _column] = 9;
                            }
                            else
                                if ((m_iTemp >= 27) && (m_iTemp < 30))
                                    map[ID, _row, _column] = 10;
                        }
                    }
                    Item.m_bAlive = true;
                    Item.m_iRow = _row;
                    Item.m_iColumn = _column;
                    Item.m_v2Position = new Vector2(17 + 40 * _column, 40 + Item.m_iy + 40 * _row) - new Vector2(0, 19);
                    break;
                }
            }
        }

        public void Update(GameTime gameTime, cWitch witch, int[, ,] map, int ID, cManagerBoom arrayBoom)
        {
            foreach (citem Item in m_arrayItem)
            {
                if (Item.m_bAlive)
                {
                    if ((Item.m_iy == 2) || (Item.m_iy == -2))
                        Item.m_ih *= -1;
                    if (Item.m_iy > 2)
                    {
                        Item.m_ih = -1;
                        Item.m_iy = 1;
                    }
                    if (Item.m_iy < -2)
                    {
                        Item.m_ih = 1;
                        Item.m_iy = -1;
                    }
                    Item.m_tsTime += gameTime.ElapsedGameTime;
                    if (Item.m_tsTime > TimeSpan.FromMilliseconds(80))
                    {
                        Item.m_tsTime -= TimeSpan.FromMilliseconds(80);
                        Item.m_iy += Item.m_ih;
                    }

                    Rectangle m_rWitchRect = new Rectangle(
                        (int)witch.m_v2Position.X + 5,
                        (int)witch.m_v2Position.Y + 45,
                        30,
                        30);
                    Rectangle m_rItemRect = new Rectangle(
                            (int)Item.m_v2Position.X + 3,
                            (int)Item.m_v2Position.Y + 30,
                            30,
                            30);
                    if (m_rWitchRect.Intersects(m_rItemRect))
                    {
                        switch (map[ID, Item.m_iRow, Item.m_iColumn])
                        {
                            case 7:
                                if (arrayBoom.m_iNumber < 8)
                                    arrayBoom.m_iNumber++;
                                Game1.g_iEnergy++;
                                Game1.g_iScore += 5;
                                break;
                            case 8:
                                if (arrayBoom.m_iLength < 6)
                                    arrayBoom.m_iLength++;
                                Game1.g_iScore += 7;
                                break;
                            case 9:
                                if (witch.m_dSpeed < 4.5)
                                {
                                    Game1.g_iSpeed++;
                                    witch.m_dSpeed += 0.3;
                                }
                                Game1.g_iScore += 8;
                                break;
                            case 10:
                                if (witch.m_iLife == 2)
                                {
                                    witch.m_iLife = 3;
                                    witch.m_ttWitchLight.m_iIDFrame = 0;
                                }
                                Game1.g_iScore += 10;
                                break;
                            default:
                                break;
                        }
                        Item.m_bAlive = false;
                        map[ID, Item.m_iRow, Item.m_iColumn] = 0;
                        Item.m_iColumn = -1;
                        Item.m_iRow = -1;
                        break;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, int _row, int _column, int type, GameTime gameTime, int vitri)
        {
            if (vitri == 2)
                foreach (citem Item in m_arrayItem)
                {
                    if (Item.m_bAlive && Item.m_iRow == _column && Item.m_iColumn == _row)
                    {
                        Item.m_v2Position = new Vector2(17 + 40 * Item.m_iColumn, 40 + Item.m_iy + 40 * Item.m_iRow) - new Vector2(0, 19);
                        switch (type)
                        {
                            case 7:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item12, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 8:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item22, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 9:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item32, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 10:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item42, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            default:
                                break;
                        }
                    }
                }
            else
                foreach (citem Item in m_arrayItem)
                {
                    if (Item.m_bAlive && Item.m_iRow == _column && Item.m_iColumn == _row)
                    {
                        switch (type)
                        {
                            case 7:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position + new Vector2(0, 40), item11, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 8:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position + new Vector2(0, 40), item21, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 9:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position + new Vector2(0, 40), item31, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            case 10:
                                spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position + new Vector2(0, 40), item41, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                                break;
                            default:
                                break;
                        }
                    }
                }
        }

        public void Draw(SpriteBatch spriteBatch, int _row, int _column, int type, GameTime gameTime)
        {
            float m_fBGLayerDepth;
            foreach (citem Item in m_arrayItem)
            {
                if (Item.m_bAlive && Item.m_iRow == _column && Item.m_iColumn == _row)
                {
                    m_fBGLayerDepth = (40 * Item.m_iRow + 35) / 600.0f;
                    Item.m_v2Position = new Vector2(17 + 40 * Item.m_iColumn, 40 + Item.m_iy + 40 * Item.m_iRow) - new Vector2(0, 19);
                    switch (type)
                    {
                        case 7:
                            spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item1, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 8:
                            spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item2, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 9:
                            spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item3, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 10:
                            spriteBatch.Draw(Item.m_tTexture, Item.m_v2Position, item4, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    class citem : cSprite
    {
        public int m_iy, m_ih;
        public bool m_bAlive;
        public int m_iRow;
        public int m_iColumn;
        public TimeSpan m_tsTime;
        public citem(ContentManager content)
            : base(content.Load<Texture2D>(@"images/item"), new Vector2(0, 0))
        {
            m_bAlive = false;
            m_iy = 0;
            m_ih = 1;
            m_iRow = -1;
            m_iColumn = -1;
            m_tsTime = TimeSpan.Zero;
        }
    }
}
